// import { useFabStore } from "@/store/modules/fabStore";
import { getHeatMap } from "@/api/machineApi";

//获取新的时间
const date = new Date();
//获取当前时间的年份转为字符串
const year = date.getFullYear();
//获取月份，由于月份从0开始，此处要加1，判断是否小于10，如果是在字符串前面拼接'0'
const month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1).toString() : (date.getMonth() + 1).toString();
//获取天，判断是否小于10，如果是在字符串前面拼接'0'
const day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
const hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(); // 获取时
const minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(); // 获取分
const seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds(); // 获取秒

const getNum = (min, max) => {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min + 1)) + min;
};

// 模拟获取实时数据
export const fetchRealtimeData = async () => {
  // const store = useFabStore();
  const queryTimeEnd = `${year}-${month}-${day} ${hour}:${minutes}:${seconds}`;
  const res: any = await getHeatMap({ queryTimeEnd });

  // 模拟数据变化
  return res.map(area => ({
    name: area.zone,
    // ...area
    x: Number(area.x),
    y: Number(area.y),
    valueNewTemp: area.valueNewTemp ? area.valueNewTemp : getNum(0, 25),
    valueNewHumidity: area.valueNewHumidity ? area.valueNewHumidity : getNum(0, 55),
    indicatorTemp: area.indicatorTemp ? area.indicatorTemp : "2_B1_HWS1_PIT1_I_AI_HMI",
    valueMaxTemp: area.valueMaxTemp ? area.valueMaxTemp : getNum(0, 25),
    valueMinTemp: area.valueMinTemp ? area.valueMinTemp : getNum(0, 25),
    valueAvgTemp: area.valueAvgTemp ? area.valueAvgTemp : getNum(0, 25),
    oocTemp: area.oocTemp,
    indicatorHumidity: area.indicatorHumidity ? area.indicatorHumidity : "CUS_HHW_MRWP",
    valueMaxHumidity: area.valueMaxHumidity ? area.valueMaxHumidity : getNum(0, 55),
    valueMinHumidity: area.valueMinHumidity ? area.valueMinHumidity : getNum(0, 55),
    valueAvgHumidity: area.valueAvgHumidity ? area.valueAvgHumidity : getNum(0, 55),
    oocHumidity: area.oocHumidity
  }));
};

// 模拟获取历史数据
export const fetchHistoryData = async (datetime: Date) => {
  // const store = useFabStore();
  const res: any = await getHeatMap({ queryTimeEnd: datetime });

  // 基于日期生成一些伪随机变化
  // const hour = new Date(datetime).getHours();
  // const minute = new Date(datetime).getMinutes();
  // const timeFactor = hour + minute / 60;

  return res.map(area => ({
    name: area.zone,
    // ...area
    x: Number(area.x),
    y: Number(area.y),
    valueNewTemp: area.valueNewTemp ? area.valueNewTemp : getNum(0, 25),
    valueNewHumidity: area.valueNewHumidity ? area.valueNewHumidity : getNum(0, 55),
    indicatorTemp: area.indicatorTemp ? area.indicatorTemp : "2_B1_HWS1_PIT1_I_AI_HMI",
    valueMaxTemp: area.valueMaxTemp ? area.valueMaxTemp : getNum(0, 25),
    valueMinTemp: area.valueMinTemp ? area.valueMinTemp : getNum(0, 25),
    valueAvgTemp: area.valueAvgTemp ? area.valueAvgTemp : getNum(0, 25),
    oocTemp: area.oocTemp,
    indicatorHumidity: area.indicatorHumidity ? area.indicatorHumidity : "CUS_HHW_MRWP",
    valueMaxHumidity: area.valueMaxHumidity ? area.valueMaxHumidity : getNum(0, 55),
    valueMinHumidity: area.valueMinHumidity ? area.valueMinHumidity : getNum(0, 55),
    valueAvgHumidity: area.valueAvgHumidity ? area.valueAvgHumidity : getNum(0, 55),
    oocHumidity: area.oocHumidity
  }));
};
